package com.xx.job.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.xx.job.common.util.MD5Util;
import com.xx.job.controller.dto.PageDTO;
import com.xx.job.entity.base.BaseEntity;
import com.xx.job.entity.system.SysDept;
import com.xx.job.entity.system.SysPost;
import com.xx.job.entity.system.SysRole;
import com.xx.job.entity.system.SysUser;
import com.xx.job.mapper.SysUserMapper;
import com.xx.job.service.system.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangcy
 * @since 2021-01-14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysPostService sysPostService;

    @Autowired
    private SysDeptService sysDeptService;
    
    

    public SysUser verifyUserNameAndPassWord(SysUser sysUser){
        return sysUserMapper.selectByNameAndPassWord(sysUser);
    }

    public Map<String,Object> selectUserList(PageDTO pageDTO, SysUser sysUser){
        Map<String,Object> data = new HashMap<>();
        IPage<SysUser> page = new Page<>(pageDTO.getPageIndex(), pageDTO.getPageSize());
        QueryWrapper<SysUser> wrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(sysUser.getUsername())){
            wrapper.like("username",sysUser.getUsername().trim());
        }
        if(StringUtils.isNotBlank(sysUser.getPhone())){
            wrapper.like("phone",sysUser.getPhone().trim());
        }
        if(StringUtils.isNotBlank(sysUser.getStatus())){
            wrapper.like("status",sysUser.getStatus().trim());
        }
        if(sysUser.getDeptId() != null){
            wrapper.eq("dept_id",sysUser.getDeptId());
        }

        IPage<SysUser> sysUserIPage = sysUserMapper.selectPage(page, wrapper);
        data.put("count",sysUserIPage.getTotal());
        List<SysUser> records = sysUserIPage.getRecords();
        for (SysUser record : records) {
            if(record.getDeptId() != null){
                SysDept sysDept = sysDeptService.selectSysDept(record.getDeptId());
                if(sysDept != null){
                    record.setDeptName(sysDept.getDeptName());
                }
            }
        }
        data.put("list",records);
        data.put("pageIndex",pageDTO.getPageIndex());
        data.put("pageSize", pageDTO.getPageSize());
        return data;
    }

    @Override
    public Map<String, Object> selectUserParam() {
        Map<String, Object> data = new HashMap<>();
        List<SysRole> sysRoles = sysRoleService.sysRoleList();
        List<SysPost> sysPosts = sysPostService.sysPostList();
        data.put("posts",sysPosts);
        data.put("roles",sysRoles);
        return data;
    }

    @Override
    public int addSysUser(SysUser sysUser) {
        if(StringUtils.isNotBlank(sysUser.getPassword())){
            String password = MD5Util.MD5(sysUser.getPassword());
            sysUser.setPassword(password);
        }
        return sysUserMapper.insert(sysUser);
    }

    @Override
    public int update(SysUser sysUser) {
        if(StringUtils.isNotBlank(sysUser.getPassword())){
            String password = MD5Util.MD5(sysUser.getPassword());
            sysUser.setPassword(password);
        }
        return sysUserMapper.updateById(sysUser);
    }

    @Override
    public int delete(Long id) {
        return sysUserMapper.deleteById(id);
    }

    @Override
    public int delete(List<Long> ids) {
        return sysUserMapper.deleteBatchIds(ids);
    }

    @Override
    public Map<String, Object> selectUser(Long id) {
        Map<String, Object> data = new HashMap<>();
        SysUser sysUser = sysUserMapper.selectById(id);
        data.put("code",200);
        data.put("data",sysUser);
        List<SysRole> sysRoles = sysRoleService.sysRoleList();
        List<SysPost> sysPosts = sysPostService.sysPostList();
        data.put("posts",sysPosts);
        data.put("roles",sysRoles);
        List<Long> postIds = new ArrayList<>();
        postIds.add(sysUser.getPostId());
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(sysUser.getRoleId());
        data.put("postIds",postIds);
        data.put("roleIds",roleIds);
        return data;
    }


}
